package luo.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import luo.Mapper.RadarLogMapper;
import luo.Service.RadarLogService;
import luo.entity.RadarLog;
import luo.utils.NameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/6/6/15:20
 **/
@Service
public class RadarLogServiceImpl implements RadarLogService {
    @Autowired
    private RadarLogMapper radarLogMapper;

    @Override
    public Integer insertRadarLog(String filename) {
        String currentTime = NameUtils.getCurrentName(filename);
        return radarLogMapper.insertRadarLog(filename, currentTime);
    }

    /**
     * @return java.util.List<luo.entity.RadarLog>
     * @description 获取最新的10条日志并且修改文件名, 仅返回时间戳部分
     * @author let_free
     * @date 2024/9/17 16:21
     */
    public List<String> getLatestTenLog() {
        QueryWrapper<RadarLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id").last("limit 10");
        List<RadarLog> result = radarLogMapper.selectList(queryWrapper);
        List<String> fileNames = result.stream().map(RadarLog::getCurrentName).collect(Collectors.toList());
//        首次查询可能出现数据库为空  这种时候填充记录到十条即可
        if (fileNames.size() < 10) {
            String lastName = fileNames.get(fileNames.size() - 1);
            int count = 10 - fileNames.size();
            for (int i = 0; i < count; i++) {
                fileNames.add(lastName);
            }
        }
        Collections.sort(fileNames);
        return fileNames;
    }

    @Override
    public String getLatestName() {
        QueryWrapper<RadarLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id").last("limit 1");
        RadarLog result = radarLogMapper.selectOne(queryWrapper);
        return result.getCurrentName();

    }

    @Override
    public List<String> getFuture20Name(String prefix, String lastName, String format, Integer len) throws ParseException {
        String datePart = lastName.substring(0, 8);  // 年月日
        String timePart = lastName.substring(8);     // 小时和分钟

        // 创建日期格式化对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");

        // 将日期和时间部分转换为 Calendar 对象
        Calendar lastNpyDateTime = Calendar.getInstance();
        lastNpyDateTime.setTime(dateFormat.parse(datePart + timePart));

        // 初始化文件名列表
        List<String> futureNpyFilenames = new ArrayList<>();

        // 添加未来两个小时（每隔六分钟）的 npy 文件名
        for (int i = 1; i <= len; i++) {  // 从 1 开始，因为已经有一个时间点
            // 计算未来时间
            Calendar futureTime = (Calendar) lastNpyDateTime.clone();
            futureTime.add(Calendar.MINUTE, 6 * i);

            // 格式化新的文件名
            String newFilename = String.format("%s", dateFormat.format(futureTime.getTime()));
            futureNpyFilenames.add(newFilename);
        }
        return futureNpyFilenames;
    }

    @Override
    public IPage<RadarLog> getHistory(Integer pageSize, Integer pageNum, String startTime, String endTime) {
        IPage<RadarLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<RadarLog> queryWrapper = new QueryWrapper<>();
        if (startTime == null && endTime == null) {
            queryWrapper.orderByDesc("id");
        } else {
            queryWrapper.orderByDesc("id").between("current_name", startTime, endTime);
        }
        return radarLogMapper.selectPage(page, queryWrapper);
    }

}
